'''
接口函数：level_analysis(end,define_num)
    end:限定范围的最大值，默认最小值为1，默认步长为1，若步长设置为0.1，可将end设为1000取得等同效果
    define_num:选取原料的种类数
函数返回：所有可能的配比组合{list}
    二维列表，array[index]为一种组合，如[1,99]
'''
from itertools import permutations
import numpy as np

def size(p):
    lenth = 0
    for j in range(len(p)):
        if p[j]!=0:
            lenth+=1
    return lenth

def output(num,out_nums):
    out_array=[]
    for j in range(len(num)):
        if num[j]!=0:
            out_array.append(num[j])
    if len(out_array)==out_nums:
        return out_array

'''
_minNum: 最小的被加的数
_sumLeave: 和的剩余值
_arrCurBound: 存放被加的数的数组当前下标
'''
def searchJoinNum(_minNum,_sumLeave,_arrCurBound,N,define_num):
    minNum = _minNum
    sumLeave = _sumLeave
    arrCurBound=_arrCurBound
    j=arrCurBound
    nums=size(num)
    if nums>define_num:
        num[arrCurBound-1]=0
        return
    if sumLeave==0:
        if output(num,define_num)!=None:
            array.append(output(num,define_num))
        num[arrCurBound - 1] = 0
        return

    if sumLeave<minNum:
        num[arrCurBound - 1] = 0
        return

    for i in range(minNum,N):
        temp=sumLeave
        num[j]=i
        sumLeave-=i
        searchJoinNum(i+1,sumLeave,j+1,N,define_num)
        sumLeave=temp

def permutation(_array):
    linshi=[]
    if len(np.array(_array).shape)>1:
        for i in range(len(_array)):
            a = list(permutations(_array[i]))
            for j in range(len(a)):
                linshi.append(list(a[j]))
    if len(np.array(_array).shape)==1:
        a=list(permutations(_array))
        for j in range(len(a)):
            linshi.append(list(a[j]))
    _array+=linshi

def delete(_array):
    if len(np.array(_array).shape) > 1:
        for s in _array:
            if _array.count(s)>1:
                _array.remove(s)
    return _array

def level_analysis(define_num, steps):
    global num,array
    end = 100
    end = int(end/steps)
    array = []
    num = [0] * end
    searchJoinNum(1,end,0,end,define_num)
    x=len(array)
    if define_num>1:
        permutation(array)
    if len(np.array(array).shape) > 1 and x>0:
        if (array[x-1][0]+steps)*define_num==100:
            linshi3=[array[x-1][0]+steps]*define_num
            array.append(linshi3)
    array=delete(array)
    for i in range(len(array)):
        array[i] = [j * steps for j in array[i]]
    return array

# 输入原料个数和配比步长
# print(level_analysis(4, 2))
